package net.vsame.url2sql.domain;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.vsame.url2sql.dao.ConnectionUtils;

public class Url2SqlContext {
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private String url;
	private String[] restfulParams;
	private Map<String, String[]> params;//参数
	private UrlConfig urlConfig;
	
	public Connection getConn() {
		if(conn == null){
			System.out.println("取得连接");
			conn = ConnectionUtils.getConnection();
		}
		return conn;
	}
	public void setConn(Connection conn) {
		this.conn = conn;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public Map<String, String[]> getParams() {
		return params;
	}
	public String[] getParams(Object key){
		return params.get(key);
	}
	public String getParam(Object key){
		return params.get(key)[0];
	}
	
	public void setParams(Map<String, String[]> params) {
		this.params = params;
	}
	public UrlConfig getUrlConfig() {
		return urlConfig;
	}
	public void setUrlConfig(UrlConfig urlConfig) {
		this.urlConfig = urlConfig;
	}
	
	/**
	 * 设置参数
	 * @param restfulParam
	 */
	public void setRestfulParam(String restfulParam) {
		@SuppressWarnings("unchecked")
		Map<String, String[]> paraMap = request.getParameterMap();
		String[] arrays = restfulParam.split("_");
		params = new HashMap<String, String[]>(paraMap.size()+arrays.length, 1);
		if(!"".equals(restfulParam)){
			for(int i=0; i<arrays.length; i++){
				String[] temp = new String[1];
				temp[0] = arrays[i];
				params.put(i + "", temp);
			}
		}
		params.putAll(paraMap);
		this.restfulParams = arrays;
	}
	public String[] getRestfulParams() {
		return this.restfulParams;
	}
	public String getRestfulParam(int i) {
		if(i<this.restfulParams.length){
			return this.restfulParams[i];
		}
		return null;
	}

}
